package com.qingzhuge.member.controller;

import com.qingzhuge.common.HttpCode;
import com.qingzhuge.common.annotation.SecurityParameter;
import com.qingzhuge.common.validator.ValidatorUtil;
import com.qingzhuge.controller.base.AbstractController;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.member.dto.response.SysMemberAuthDto;
import com.qingzhuge.member.dto.response.SysMemberDto;
import com.qingzhuge.member.entity.SysMember;
import com.qingzhuge.member.entity.SysMemberConfig;
import com.qingzhuge.member.entity.SysMemberVerification;
import com.qingzhuge.member.service.ISysMemberCloundAuthService;
import com.qingzhuge.member.service.ISysMemberService;
import com.qingzhuge.verification.dto.CloudAuthRequest;
import com.qingzhuge.verification.enums.EnumCloudType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * @author : zero.xiao
 * @description :
 * @date :2018/5/4 0004 下午 16:22
 * @modified :
 */
@RestController
@RequestMapping("/api/auth/member")
@Api(tags = "A会员页面")
public class ApiMemberController extends AbstractController {
    @Resource
    private ISysMemberService memberService;
    @Resource
    private ISysMemberCloundAuthService memberCloundAuthService;

    @PostMapping
    @ApiOperation(value = "修改用户信息")
    public ResponseEntity<ResponseBodyDto<Object>> doProfileEdit(@RequestBody @Valid SysMember member) {
        member.setId(getUid());
        if (null != memberService.save(member)) {
            return returnSuccess();
        } else {
            return returnFailed("操作失败");
        }
    }

    @GetMapping
    @ApiOperation(value = "当前登录会员")
    @SecurityParameter
    public ResponseEntity<ResponseBodyDto<SysMemberDto>> doUserInfo() {
        SysMember member = memberService.findById(this.getUid());
        SysMemberDto sysMemberDto = memberService.bean2Dto(member, SysMemberDto.class);
        return returnSuccess(sysMemberDto);
    }


    @GetMapping("{mobile}")
    @ApiOperation(value = "按手机号获取用户信息")
    public ResponseEntity<ResponseBodyDto<SysMemberDto>> doUserInfoByMobile(@PathVariable String mobile) {
        SysMember member = memberService.findByMobile(mobile);
        SysMemberDto sysMemberDto = memberService.bean2Dto(member, SysMemberDto.class);
        return returnSuccess(sysMemberDto);
    }

    @PutMapping("{mobile}")
    @ApiOperation(value = "修改手机号")
    public ResponseEntity<ResponseBodyDto<SysMemberDto>> doUpdateMobile(@PathVariable String mobile) {
        memberService.updateMobile(getUid(), mobile);
        SysMember member = memberService.findByMobile(mobile);
        SysMemberDto sysMemberDto = memberService.bean2Dto(member, SysMemberDto.class);
        return returnSuccess(sysMemberDto);
    }

    @PostMapping("id_card_auth")
    @ApiOperation(value = "实人认证")
    public ResponseEntity<ResponseBodyDto<String>> auth(@RequestBody CloudAuthRequest authRequest) {
        if (null == authRequest.getType()) {
            authRequest.setType(EnumCloudType.aliyun);
        }
        ValidatorUtil.validateEntity(authRequest);
        SysMemberVerification cloundAuth = memberCloundAuthService.auth(authRequest, new SysMember(getUid()));
        return returnSuccess(cloundAuth.getToken());
    }

    @PostMapping("id_card_auth/{token}")
    @ApiOperation(value = "获取认证结果")
    public ResponseEntity<ResponseBodyDto<SysMemberVerification>> getAuth(@PathVariable String token) {
        SysMemberVerification memberCloundAuth = memberCloundAuthService.getAuth(token);
        if (null != memberCloundAuth) {
            return returnSuccess(memberCloundAuth);
        } else {
            return returnFailed(HttpCode.TOKEN_VALIDATOR_FAILED.message());
        }
    }

    @GetMapping("id_card_auth")
    @ApiOperation(value = "用户是否认证")
    public ResponseEntity<ResponseBodyDto<SysMemberAuthDto>> idCardAuth() {
        SysMemberVerification cloundAuth = memberCloundAuthService.findByUser(getUid());
        if (null != cloundAuth) {
            SysMemberAuthDto authDto = memberCloundAuthService.bean2Dto(cloundAuth, SysMemberAuthDto.class);
            return returnSuccess(authDto);
        }
        return returnSuccess();
    }

    @PostMapping("config")
    @ApiOperation(value = "用户配置")
    public ResponseEntity<ResponseBodyDto<Object>> config(@RequestBody SysMemberConfig config) {
        if (null != config) {
            memberService.setMemberConfig(config);
            return returnSuccess();
        } else {
            return returnSuccess(memberService.getMemberConfig());
        }
    }


}
